<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/15
 * Time: 22:10
 */

namespace App\Library\form_builder\src\components;


class Radio extends BaseForm
{
    protected $type='radio';
    protected $options='';
    /**
     * 生成组件
     *
     * @return array
     */
    public function build()
    {
        return [
            'type' => $this->type,
            'field' => $this->field,
            'title' => $this->title,
            'value' => $this->value,
            'options'=>$this->options
        ];
    }
    public function options($data){
        if(!is_array($data)){
            throw new \Exception('必须是数组');
        } else{
            $html='';
            foreach ($data as $item=>$value){
                $html.="<input type='{$this->type}' name='{$this->field}' value='{$value["value"]}' title='{$value["label"]}'>";
            }

            $this->options=$html;
        }

        return $this;
    }
}